package com.itheima.thread.pool.demo01;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 线程池的类
 */
public class ThreadPool {

    private int poolSize = 2;

    private BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<Runnable>();

    public ThreadPool() {

        for(int x = 0; x < poolSize; x++) {
            new TaskThread("线程---->" + x).start();
        }

    }

    public void submit(Runnable runnable) {
        try {
            blockingQueue.put(runnable);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public class TaskThread extends Thread {

        public TaskThread(String name) {
            super(name);
        }

        @Override
        public void run() {

            while (true) {

                try {
                    Runnable runnable = blockingQueue.take();
                    runnable.run();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }
    }

}
